package com.course.business.controller.web.discusspost;

import com.course.business.controller.web.discusspost.event.EventProducer;
import com.course.server.domain.Member;
import com.course.server.domain.discusspost.Event;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.service.MemberService;
import com.course.server.service.discusspost.FollowService;
import com.course.server.util.CommunityConstant;
import com.course.server.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/web")
public class FollowController implements CommunityConstant {

    @Autowired
    private FollowService followService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private MemberService memberService;

    @Autowired
    private EventProducer eventProducer;

    @RequestMapping(path = "/follow", method = RequestMethod.POST)
    public ResponseDto follow(@RequestBody Map<String,String> map) {
        String entityType=map.get("entityType");
        String entityId=map.get("entityId");

        Member member = hostHolder.getMember();

        followService.follow(member.getId(), entityType, entityId);
        // 状态
        boolean followStatus = followService.hasFollowed(member.getId(), entityType, entityId);

        // 触发关注事件
        if(followStatus){
            Event event = new Event()
                    .setTopic(TOPIC_FOLLOW)
                    .setUserId(hostHolder.getMember().getId())
                    .setEntityType(entityType)
                    .setEntityId(entityId)
                    .setEntityUserId(entityId);
            eventProducer.fireEvent(event);
        }

        ResponseDto responseDto=new ResponseDto();
        responseDto.setContent(followStatus);
        return responseDto;
    }

    @RequestMapping(path = "/followees/{userId}")
    public ResponseDto getFollowees(@PathVariable("userId") String userId,@RequestBody  PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();

        Member user = memberService.findUserById(userId);
        if (user == null) {
            throw new RuntimeException("该用户不存在!");
        }
        Map<String,Object> dto=new HashMap<>();
        dto.put("user", user);

        pageDto.setTotal(followService.findFolloweeCount(userId, ENTITY_TYPE_USER));

        List<Map<String, Object>> userList = followService.findFollowees(userId, (pageDto.getPage()-1)*pageDto.getSize(),pageDto.getSize());
        if (userList != null) {
            for (Map<String, Object> map : userList) {
                Member u = (Member) map.get("user");
                map.put("hasFollowed", hasFollowed(userId,u.getId()));
            }
        }
        dto.put("users", userList);
        pageDto.setMap(dto);

        responseDto.setContent(pageDto);
        return responseDto;
    }

    @RequestMapping(path = "/followers/{userId}")
    public ResponseDto getFollowers(@PathVariable("userId") String userId, @RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();

        Member user = memberService.findUserById(userId);
        if (user == null) {
            throw new RuntimeException("该用户不存在!");
        }
        Map<String,Object> dto=new HashMap<>();
        dto.put("user", user);

        pageDto.setTotal((int) followService.findFollowerCount(ENTITY_TYPE_USER, userId));

        List<Map<String, Object>> userList = followService.findFollowers(userId, (pageDto.getPage()-1)*pageDto.getSize(),pageDto.getSize());
        if (userList != null) {
            for (Map<String, Object> map : userList) {
                Member u = (Member) map.get("user");
                map.put("hasFollowed", hasFollowed(userId,u.getId()));
            }
        }
        dto.put("users", userList);

        pageDto.setMap(dto);

        responseDto.setContent(pageDto);
        return responseDto;
    }

    private boolean hasFollowed(String userId1,String  userId2) {
        return followService.hasFollowed(userId1, ENTITY_TYPE_USER, userId2);
    }

}
